﻿#include "scan_view.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font_mngr.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "math.h"
#include "m_product_test.h"
#include "display_interface.h"
#include "distance_estimate.h"
#include "scan_view.inc"
// void view_scan_display(int view)
// {
//     if (disp_ctrl.loop_cnt == 0)
//     {
//         disp_ctrl.auto_refresh_time = 1000;
//         disp_ctrl.auto_close_delay = 2000;
//         memcpy(&disp_ctrl.com_data.i_temp[0], &ble_devices, sizeof(ble_devices));
//     }

//     char buffer[128] = {0};
//     uint16_t len = 16;
//     memset(buffer, 0, sizeof(buffer));
//     sd_ble_gap_device_name_get((uint8_t *)buffer, &len);
//     char *under_line = 0;
//     under_line = strchr((char const *)buffer, '_');
//     if (under_line != 0)
//     {
//         sprintf(buffer, "  %s", under_line + 1);
//         disp_show_font_string_utf8_mid(10, buffer, 0);
//     }
//     Ble_device *ble_devices = (Ble_device *)(&disp_ctrl.com_data.i_temp[0]);
//     for (int i = 0; i < BLE_DEVICE_COUNT; i++)
//     {
//         if (ble_devices[i].rssi < 0)
//         {
//             under_line = strchr((char const *)ble_devices[i].name, '_');
//             if (under_line == 0)
//             {
//                 break;
//             }
//             sprintf(buffer, "%s", under_line + 1);
//             disp_show_font_string_utf8_mid(32 + i * 32, buffer, 0);
//             sprintf(buffer, "%d   %d", ble_devices[i].rssi_filter_number, ble_devices[i].rssi);
//             disp_show_font_string_utf8_mid(32 + i * 32 + 16, buffer, 0);
//         }
//         else
//         {
//             break;
//         }
//     }
// }
// static void show_distance(void)
// {
//     uint8_t const *distance_icon = icon_1m;
//     switch (disp_ctrl.com_data.i_temp[1])
//     {
//     case 1:
//         distance_icon = icon_1m;
//         break;
//     case 2:
//         distance_icon = icon_2m;
//         break;
//     case 3:
//         distance_icon = icon_3m;
//         break;
//     default:
//         distance_icon = icon_1m;
//         set_warning_distance(100);
//         break;
//     }
//     draw_on_chip_image(30, 122, distance_icon);
// }
// static void view_scan_base_display(void)
// {
//     if (disp_ctrl.loop_cnt == 0)
//     {
//         disp_ctrl.auto_refresh_time = 1000;
//         disp_ctrl.com_data.i_temp[0] = m_sys_cfg_data.scan_mode;
//     }
//     // disp_show_font_string_utf8_mid(10, "长按设置", 0);
//     switch (disp_ctrl.com_data.i_temp[0])
//     {
//     case Scan_mode_any:
//         draw_on_chip_image(9, 60, icon_any);
//         // disp_show_font_string_utf8_mid(30, "任意设备", 0);
//         show_distance();
//         break;
//     case Scan_mode_walnutin:
//         draw_on_chip_image(9, 60, icon_walnutin);
//         // disp_show_font_string_utf8_mid(30, "同型号", 0);
//         show_distance();
//         break;
//     default:
//         // disp_show_font_string_utf8_mid(30, "未启用", 0);
//         set_scan_mode(Scan_mode_off);
//         disp_ctrl.com_data.i_temp[0] = m_sys_cfg_data.scan_mode;
//         draw_on_chip_image(9, 60, icon_close);
//         break;
//     }
// }
// static void view_scan_set_mode_display(void)
// {
//     if (disp_ctrl.loop_cnt == 0)
//     {
//         disp_ctrl.auto_refresh_time = 1000;
//     }
//     uint8_t const *icon = icon_any;
//     disp_show_font_string_utf8_mid(0, "设置模式", 0);
//     disp_show_font_string_utf8_mid(20, "短按切换", 0);
//     disp_show_font_string_utf8_mid(40, "长按确认", 0);
//     switch (disp_ctrl.com_data.i_temp[0])
//     {
//     case Scan_mode_any:
//         icon = icon_any;
//         break;
//     case Scan_mode_walnutin:
//         icon = icon_walnutin;
//         break;
//     default:
//         disp_ctrl.com_data.i_temp[0] = Scan_mode_off;
//         icon = icon_close;
//         break;
//     }
//     draw_on_chip_image(9, 80, icon);
// }
// static void view_scan_set_distance_display(void)
// {
//     if (disp_ctrl.loop_cnt == 0)
//     {
//         disp_ctrl.auto_refresh_time = 1000;
//     }
//     disp_show_font_string_utf8_mid(0, "设置距离", 0);
//     disp_show_font_string_utf8_mid(20, "短按切换", 0);
//     disp_show_font_string_utf8_mid(40, "长按确认", 0);
//     uint8_t const *icon = icon_any;
//     switch (disp_ctrl.com_data.i_temp[0])
//     {
//     case 100:
//         icon = icon_1m;
//         break;
//     case 200:
//         icon = icon_2m;
//         break;
//     case 300:
//         icon = icon_3m;
//         break;
//     default:
//         disp_ctrl.com_data.i_temp[0] = 100;
//         icon = icon_close;
//         break;
//     }
//     draw_on_chip_image(30, 80, icon);
// }
// view_event_result_t view_base_event(event_param_t const *param)
// {
//     return view_event_result_not_handled;
// }
// view_event_result_t view_scan_set_mode_event(event_param_t const *param)
// {
//     switch (param->event)
//     {
//     case event_view_entry:
//         disp_ctrl.com_data.i_temp[0] = m_sys_cfg_data.scan_mode;
//         return view_event_result_handled;
//     case event_button_front_press:
//         disp_ctrl.com_data.i_temp[0] += 1;
//         if (disp_ctrl.com_data.i_temp[0] >= Scan_mode_end)
//         {
//             disp_ctrl.com_data.i_temp[0] = 0;
//         }
//         display_set_update_request(View_scan_set_mode, 0);
//         return view_event_result_handled;
//     case event_button_front_long_press:
//         set_scan_mode((Scan_mode)disp_ctrl.com_data.i_temp[0]);
//         if (disp_ctrl.com_data.i_temp[0] == Scan_mode_off)
//         {
//             display_show(View_scan_base, DISP_MODE_DOWN_MOVE);
//         }
//         else
//         {
//             display_show(View_scan_set_distance, DISP_MODE_DOWN_MOVE);
//         }

//         return view_event_result_handled;
//     }
//     return view_event_result_not_handled;
// }
// view_event_result_t view_scan_set_distance_event(event_param_t const *param)
// {
//     switch (param->event)
//     {
//     case event_button_front_press:
//         disp_ctrl.com_data.i_temp[0] += 100;
//         if (disp_ctrl.com_data.i_temp[0] >= 400)
//         {
//             disp_ctrl.com_data.i_temp[0] = 100;
//         }
//         display_set_update_request(View_scan_set_distance, 0);
//         return view_event_result_handled;
//     case event_button_front_long_press:
//         set_warning_distance(disp_ctrl.com_data.i_temp[0]);
//         display_show(View_scan_base, DISP_MODE_DOWN_MOVE);
//         return view_event_result_handled;
//     }
//     return view_event_result_not_handled;
// }
// void view_scan_display(int view)
// {
//     switch (view)
//     {
//     case View_scan_base:
//         view_scan_base_display();
//         break;
//     case View_scan_set_mode:
//         view_scan_set_mode_display();
//         break;
//     case View_scan_set_distance:
//         view_scan_set_distance_display();
//         break;
//     }
// }
// view_event_result_t view_scan_event(int view, event_param_t const *param)
// {
//     view_event_result_t result = view_event_result_not_handled;
//     switch (view)
//     {
//     case View_scan_base:
//         result = view_base_event(param);
//         break;
//     case View_scan_set_mode:
//         result = view_scan_set_mode_event(param);
//         break;
//     case View_scan_set_distance:
//         result = view_scan_set_distance_event(param);
//         break;
//     }
//     if (result == view_event_result_handled)
//     {
//         return result;
//     }
//     return view_event_result_not_handled;
// }
static void view_scan_mode_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[0] = m_sys_cfg_data.scan_mode;
    }
    uint8_t const *icon = icon_any;
    switch (disp_ctrl.com_data.i_temp[0])
    {
    case Scan_mode_any:
        icon = icon_any;
        draw_on_chip_image(9, 60, icon);
        break;
    case Scan_mode_walnutin:
        icon = icon_walnutin;
        draw_on_chip_image(9, 60, icon);
        break;
    default:
        disp_ctrl.com_data.i_temp[0] = Scan_mode_off;
        set_scan_mode((Scan_mode)disp_ctrl.com_data.i_temp[0]);
        icon = icon_stop;
        draw_on_chip_image(4, 45, icon);
        break;
    }
}
static void view_scan_distance_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[0] = m_sys_cfg_data.warning_distance;
    }
    uint8_t const *icon = icon_any;
    switch (disp_ctrl.com_data.i_temp[0] / 10)
    {
    case 1:
        icon = icon_1m;
        draw_on_chip_image(30, 73, icon);
        break;
    case 2:
        icon = icon_2m;
        draw_on_chip_image(30, 73, icon);
        break;
    case 3:
        icon = icon_3m;
        draw_on_chip_image(30, 73, icon);
        break;
    default:
        disp_ctrl.com_data.i_temp[0] = 0;
        set_warning_distance(disp_ctrl.com_data.i_temp[0]);
        icon = icon_off;
        draw_on_chip_image(4, 45, icon);
        break;
    }
}
void view_scan_display(int view)
{
    switch (view)
    {
    case View_scan_mode:
        view_scan_mode_display();
        break;
    case View_scan_distance:
        view_scan_distance_display();
        break;
    }
}
view_event_result_t view_scan_mode_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_button_front_press:
        display_show(View_scan_distance, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    case event_button_front_long_press:
        disp_ctrl.com_data.i_temp[0] += 1;
        if (disp_ctrl.com_data.i_temp[0] >= Scan_mode_end)
        {
            disp_ctrl.com_data.i_temp[0] = 0;
        }
        set_scan_mode((Scan_mode)disp_ctrl.com_data.i_temp[0]);
        display_set_update_request(View_scan_mode, 0);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_scan_distance_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_button_front_press:
        display_show(View_scan_mode, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    case event_button_front_long_press:
        disp_ctrl.com_data.i_temp[0] += 10;
        if (disp_ctrl.com_data.i_temp[0] > 30)
        {
            disp_ctrl.com_data.i_temp[0] = 0;
        }
        set_warning_distance(disp_ctrl.com_data.i_temp[0] * 10);
        display_set_update_request(View_scan_distance, 0);
        // set_warning_distance(disp_ctrl.com_data.i_temp[0]);
        // display_show(View_scan_base, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_scan_event(int view, event_param_t const *param)
{
    view_event_result_t result = view_event_result_not_handled;
    switch (view)
    {
    case View_scan_mode:
        result = view_scan_mode_event(param);
        break;
    case View_scan_distance:
        result = view_scan_distance_event(param);
        break;
    }
    if (result == view_event_result_handled)
    {
        return result;
    }
    return view_event_result_not_handled;
}
